#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Author :   Liang Kang
@Contact:   gangkanli1219@163.com
@Time   :   2018/8/7 20:08
@Desc   :
"""
import argparse
from pathlib import Path

from dltools.utils.log import get_console_logger

from shape.data.cropper import ShapeCropper
from utils.file_read import GoodsReader


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--root', type=str, default='', dest='root',
        help='保存图像与标记数据的根目录, 图像与其相应标记应当在同一级目录。')
    parser.add_argument(
        '--output', type=str, default='', dest='output',
        help='输出结果的根目录。')
    parser.add_argument(
        '--ratio', type=float, default=0.1, dest='ratio',
        help='放大的比例。')
    parser.add_argument(
        '--image-file', type=str, default='', dest='image',
        help='保持图像的路径和标签。')
    parser.add_argument(
        '--break-file', type=str, default='', dest='file',
        help='保存损坏的图像。')
    return parser.parse_args()


def crop():
    reader = GoodsReader(ARGS.root, logger)
    output = Path(ARGS.output)
    if not output.exists():
        output.mkdir()
    image_file = ARGS.image
    ratio = ARGS.ratio
    break_file = ARGS.file
    for idx, data in enumerate(reader):
        cropper = ShapeCropper(data['image'], data['xml'],
                               output, image_file, ratio,
                               logger=logger, break_file=break_file)
        cropper.update()


if __name__ == '__main__':
    ARGS = parse_args()
    logger = get_console_logger('Cropper')
    crop()
